class Product:
    def __init__(self, name, quantity, price):
        self.name = name
        self.quantity = quantity
        self.price = price
        
    def get_amount(self):
        """计算商品总金额"""
        return self.quantity * self.price


class Receipt:
    def __init__(self, receipt_id, cashier, discount=1.0):
        self.receipt_id = receipt_id
        self.cashier = cashier
        self.discount = discount  # 折扣率，1.0表示无折扣
        self.products = []
        # 获取当前时间
        from datetime import datetime
        self.time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        
    def add_product(self, product):
        """添加商品到购物小票"""
        self.products.append(product)
        
    def calculate_total(self):
        """计算商品总额"""
        return sum(product.get_amount() for product in self.products)
    
    def calculate_discounted_total(self):
        """计算折扣后总额"""
        return self.calculate_total() * self.discount
    
    def print_receipt(self):
        """打印购物小票，确保格式对齐"""
        # 打印表头信息
        print("单号: {}".format(self.receipt_id))
        print("时间: {}".format(self.time))
        print("-" * 40)
        
        # 打印列标题，设置各列宽度
        name_width = 16  # 商品名称宽度
        qty_width = 6    # 数量宽度
        price_width = 8  # 单价宽度
        amount_width = 8 # 金额宽度
        
        # 打印列标题
        print(f"{'名称'.ljust(name_width)}{'数量'.center(qty_width)}{'单价'.rjust(price_width)}{'金额'.rjust(amount_width)}")
        
        # 打印商品列表
        for product in self.products:
            # 商品名称左对齐，数量居中，单价和金额右对齐
            name_str = product.name.ljust(name_width)
            qty_str = str(product.quantity).center(qty_width)
            price_str = f"{product.price:.2f}".rjust(price_width)
            amount_str = f"{product.get_amount():.2f}".rjust(amount_width)
            
            print(f"{name_str}{qty_str}{price_str}{amount_str}")
        
        print("-" * 40)
        
        # 计算总计信息
        total = self.calculate_total()
        discounted_total = self.calculate_discounted_total()
        
        # 打印总计信息，右对齐显示金额
        print(f"总数: {len(self.products):>{name_width + qty_width}}")
        print(f"总额: {total:.2f}".rjust(name_width + qty_width + price_width + amount_width))
        print(f"折扣: 9.9折")
        print(f"折后总额: {discounted_total:.2f}".rjust(name_width + qty_width + price_width + amount_width))
        print(f"实收: {discounted_total:.2f}".rjust(name_width + qty_width + price_width + amount_width))
        print(f"找零: 0.00".rjust(name_width + qty_width + price_width + amount_width))
        print(f"收银: {self.cashier}")
        print("-" * 40)


# 示例用法
if __name__ == "__main__":
    # 创建购物小票
    receipt = Receipt("DH202311010001", "管理员")
    
    # 添加商品
    receipt.add_product(Product("金士顿U盘8G", 1, 40.00))
    receipt.add_product(Product("胜创16GTF卡", 1, 50.00))
    receipt.add_product(Product("读卡器", 1, 8.00))
    receipt.add_product(Product("网线2米", 1, 5.00))
    receipt.add_product(Product("脆升升", 1, 6.00))
    
    # 打印小票
    receipt.print_receipt()
